﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 工作时间定义
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class WorkTimeController : AdminBaseController
    {
        /// <summary>
        /// 工作时间定义首页
        /// </summary>
        /// <returns>首页页面</returns>
        [AuthorityActionFilter("m_WorkTime", Order = 1)]
        [LogPageActionFilter("工作时间定义", Order = 2)]
        public ActionResult Index()
        {
            BLL.WorkTime workTimeBLL = new BLL.WorkTime();
            List<Model.WorkTime> workTimeList = GetWorkTimeList("",1);
            return View(workTimeList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.WorkTime> GetWorkTimeList(string strWhere, int pageIndex)
        {
            BLL.WorkTime workTimeBll = new BLL.WorkTime();

            string fiedls = "*";
            int total;
            List<ElingOA6_Admin_DSJCJ.Model.WorkTime> workTimeList = workTimeBll.GetWorkTimeListByPage("t_WorkTime", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, "", "FWorkTimeID", 1, out total);

            List<Model.WorkTime> workTimeAllList = new List<Model.WorkTime>();

            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                workTimeAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (workTimeList.Count > i)
                {
                    workTimeAllList[((pageIndex - 1) * pageRowSize) + i] = workTimeList[i];
                }
            }

            return workTimeAllList;
        }

        /// <summary>
        /// 修改或增加定义的工作时间页面初始化
        /// </summary>
        /// <param name="fWorkTimeID">时间编号</param>
        /// <returns>初始化页面</returns>
        public ActionResult EditWorkTime(string fWorkTimeID)
        {
            Model.WorkTime workTimeModel = new Model.WorkTime();
            if (fWorkTimeID != null && fWorkTimeID != "")
            {
                BLL.WorkTime workTimeBLL = new BLL.WorkTime();
                workTimeModel = workTimeBLL.GetModel(decimal.Parse(fWorkTimeID));

                ViewData["hdMethod"] = "Edit";
                ViewData["jsAlert"] = "";
                ViewData["FWorkTimeID"] = fWorkTimeID;
            }
            else
            {
                ViewData["hdMethod"] = "ADD";
                ViewData["jsAlert"] = "";
                ViewData["FWorkTimeID"] = "";
            }

            return View(workTimeModel);
        }
        /// <summary>
        /// 工作时间定义Grid调用方法
        /// </summary>
        /// <returns>Grid页面</returns>
        public ActionResult WorkTimeListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();
            string type = Request.Params["Type"];
            string gNO = Request.Params["gNO"];
            string dutyNo = Request.Params["DutyNo"];

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }

            List<Model.WorkTime> workTimeList = GetWorkTimeList(command, pageIndex);


            return PartialView("WorkTimeListPartial", workTimeList);
        }
       /// <summary>
       /// 保存方法
       /// </summary>
       /// <param name="workTimeModel">保存数据</param>
       /// <returns>保存后跳转页面</returns>
        public ActionResult Save(Model.WorkTime workTimeModel)
        {
            try
            {
                if (workTimeModel.FBeginDate > workTimeModel.FEndDate)
                {
                    ViewData["jsAlert"] = "结束时间不可小于开始时间";
                }
                else if (workTimeModel.FAMAttendanceTime > workTimeModel.FPMAttendanceTime)
                {
                    ViewData["jsAlert"] = "上午考勤时间不可大于下午考勤时间";
                }
                else
                {
                    BLL.WorkTime workTimeBLL = new BLL.WorkTime();
                    workTimeModel.FIsModify = "N";             
                    switch (Fetch.Post("hdMethod"))
                    {
                        case "ADD":
                            workTimeModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                            workTimeBLL.Add(workTimeModel);
                            break;
                        case "Edit":
                            workTimeModel.FWorkTimeID = decimal.Parse(Fetch.Post("FWorkTimeID"));
                            workTimeBLL.Update(workTimeModel);
                            break;
                    }
                    ViewData["jsAlert"] = "OK";
                }
            }
            catch
            {
                ViewData["jsAlert"] = "error";
            }

            ViewData["hdMethod"] = Fetch.Post("hdMethod");
            ViewData["FWorkTimeID"] = workTimeModel.FWorkTimeID;

            return View("EditWorkTime", workTimeModel);
        }

       /// <summary>
       /// 删除定义的工作时间（单个）
       /// </summary>
       /// <param name="fWorkTimeID">时间编号</param>
       /// <returns>是否成功</returns>
        public string DelEditWork(string fWorkTimeID)
        {
            try
            {
                BLL.WorkTime workTimeBLL = new BLL.WorkTime();
                workTimeBLL.Delete(decimal.Parse(fWorkTimeID), Common.GlobalConstant.ENTITYNO);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        /// <summary>
        /// 删除定义的工作时间（批量）
        /// </summary>
        /// <param name="workTimeNoArray">时间编号以逗号分隔</param>
        /// <returns>是否成功</returns>
        public string DeleteWorkTime(string workTimeNoArray)
        {
            try
            {
                BLL.WorkTime workTimeBLL = new BLL.WorkTime();
                workTimeBLL.DeleteList(workTimeNoArray);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }
        
    }
}
